(function($) {
	"use strict";

    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
        checkboxClass: 'icheckbox_minimal-purple',
        radioClass: 'iradio_minimal-purple'
    });

	//cover images
	$( ".cover-image").each(function() {
		  var attr = $(this).attr('data-image-src');
		
		  if (typeof attr !== typeof undefined && attr !== false) {
			  $(this).css('background', 'url('+attr+') center center');
		  }
	});
	
	//cover image2
	$( ".cover-image2").each(function() {
		  var attr = $(this).attr('data-image-src');
		
		  if (typeof attr !== typeof undefined && attr !== false) {
			  $(this).css('background', 'url('+attr+') center center');
		  }
	});
	
	//mCustomScrollbar
	$(".app-sidebar").mCustomScrollbar({
		theme:"minimal",
		autoHideScrollbar: true,
		scrollbarPosition: "outside"
	});

	//PAGE LOADING
	$(window).on("load", function(e) {
		$("#spinner").fadeOut("slow");
	})
	
	// tooltip
	$("[data-toggle='tooltip']").tooltip();

	// popover
	$('[data-toggle="popover"]').popover({
		container: 'body'
	});

	/*----SideBar----*/
	$(".app-sidebar a").each(function() {
	  var pageUrl = window.location.href.split(/[?#]/)[0];
		if (this.href == pageUrl) { 
			$(this).addClass("active");
			$(this).parent().addClass("active"); // add active to li of the current link
			$(this).parent().parent().prev().addClass("active"); // add active class to an anchor
			$(this).parent().parent().prev().click(); // click the item to make it drop
		}
	});
	

	/*----FullScreen----*/
	$(document).on("click","#fullscreen-button", function toggleFullScreen() {
		if ((document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen)) {
			if (document.documentElement.requestFullScreen) {
			  document.documentElement.requestFullScreen();
			} else if (document.documentElement.mozRequestFullScreen) {
			  document.documentElement.mozRequestFullScreen();
			} else if (document.documentElement.webkitRequestFullScreen) {
			  document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
			} else if (document.documentElement.msRequestFullscreen) {
			  document.documentElement.msRequestFullscreen();
			}
		} else {
			if (document.cancelFullScreen) {
			  document.cancelFullScreen();
			} else if (document.mozCancelFullScreen) {
			  document.mozCancelFullScreen();
			} else if (document.webkitCancelFullScreen) {
			  document.webkitCancelFullScreen();
			} else if (document.msExitFullscreen) {
			  document.msExitFullscreen();
			}
		}
	})

	/*----GlobalSearch----*/
	$(document).on("click", "[data-toggle='search']", function(e) {
		var body = $("body");

		if(body.hasClass('search-gone')) {
			body.addClass('search-gone');
			body.removeClass('search-show');
		}else{
			body.removeClass('search-gone');
			body.addClass('search-show');
		}
	});
	var toggleSidebar = function() {
		var w = $(window);
		if(w.outerWidth() <= 1024) {
			$("body").addClass("sidebar-gone");
			$(document).off("click", "body").on("click", "body", function(e) {
				if($(e.target).hasClass('sidebar-show') || $(e.target).hasClass('search-show')) {
					$("body").removeClass("sidebar-show");
					$("body").addClass("sidebar-gone");
					$("body").removeClass("search-show");
				}
			});
		}else{
			$("body").removeClass("sidebar-gone");
		}
	}
	toggleSidebar();
	$(window).resize(toggleSidebar);
		
	/*----CollapseableLeftMenu----*/
	$("[data-collapse]").each(function() {
		var me = $(this),
				target = me.data('collapse');

		me.click(function() {
			$(target).collapse('toggle');
			$(target).on('shown.bs.collapse', function() {
				me.html('<i class="ion ion-minus"></i>');
			});
			$(target).on('hidden.bs.collapse', function() {
				me.html('<i class="ion ion-plus"></i>');
			});
			return false;
		});
	});
	
	/*----Alerts----*/
	$(".alert-dismissible").each(function() {
		var me = $(this);

		me.find('.close').on("click", function(e) {
			me.alert('close');
		});
	});
	
})(jQuery);

function popupPage(title, url, w, h, is_parent) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (w == null || w == '') {
        w = ($(window).width() * 0.8);
    }
    if (h == null || h == '') {
        h = ($(window).height() - 50);
    }
    console.log(w,h)
    ;
    var opt = {
        type: 2,
        area: [w + 'px', h + 'px'],
        fix: false,
        // maxmin: true,
        shadeClose: false,
        shade: 0.4,
        title: title,
        content: url
    };
    if(is_parent == true) {
        parent.layer.open(opt);
	} else {
        layer.open(opt);
	}
}
// 操作按钮点击
function handleClick(obj, id, iframeHeight, iframeWidth) {
    var title = $(obj).attr('data-title'),
        url = $(obj).attr('data-url');
    if (id) url = url + '?id=' + id;
    popupPage(title, url, iframeWidth || 800, iframeHeight || 550)
}
function showBigWindow(obj, id, iframeHeight, iframeWidth) {
    handleClick(obj, id, 700, 1000)
}
// 点击跳转
function redirect(obj, id) {
    var title = $(obj).attr('data-title'),
        url = $(obj).attr('data-url');
    location.href = url + '?id=' + id;
}
// 点击跳转
function ajaxReq(obj, id) {
    var url = $(obj).attr('data-url');
	return ajaxRequest(url, {id: id}, function(res) {
		layer.msg(res.msg, {
          	// offset: 'rt',
          	time: 1000,
			end: function () {
            	window.location.reload();
          	}
		});
	}, 'POST');
}
// 点击输入后操作
function handlePrompt(obj, id) {
    var title = $(obj).attr('data-title'),
        url = $(obj).attr('data-url');
    layer.prompt({
        title: title || '请输入',
    }, function (value, index, elem) {
        ajaxRequest(url, {id: id, value: value}, '', 'POST');
        layer.close(index);
		location.reload();
    });
}
// 点击输入后操作
function handleConfirm(obj, id) {
    var title = $(obj).attr('data-title'),
        url = $(obj).attr('data-url');

    layer.msg(title+"?", {
        time: 0 //不自动关闭
        ,btn: ['Confirm', 'Cancel']
        ,yes: function(index){
            layer.close(index);
            ajaxRequest(url, {id: id}, '', 'POST');
            // location.reload();
        }
    });

}
function delOne(obj, id) {
    var t = $(obj).attr('data-title') || '删除';
    var url = $(obj).attr('data-url');
    if (url) {
        layer.confirm('确认' + t + '?', {title: 'INFO'}, function (index) {
            ajaxRequest(url, {id: id}, function (res) {
                // $(obj).parents("tr").remove();
							 if(res != 1) layer.msg('删除失败');
							 else location.reload();
            });
            layer.close(index);
        });
    } else {
        console.log('缺少url');
    }
}
function delAll(argument) {
    var data = tableCheck.getData();
    layer.confirm('确认要删除吗？' + data, {title: 'INFO'}, function (index) {
        //捉到所有被选中的，发异步进行删除
        layer.msg('SUCCESS', {icon: 1});
        $(".layui-form-checked").not('.header').parents('tr').remove();
    });
}

function ajaxRequest(url, param, cb, type) {
    var param = param || {};
    var type = type || 'GET';
    $.ajax({
        type: type,
        url: url,
        data: param,
        success: function (data) {
            //批量显示没有返回1的，就是没成功
            if (data == 1 || data.code == 1) {
                if (cb) {
                    cb(data);
                } else {
                    layer.msg('SUCCESS');
                }
            } else {
                layer.msg(data.msg || 'ERROR');
            }
            layer.closeAll('loading');
        },
        error: function () {
            layer.closeAll('loading');
            layer.msg('ERROR');
        }
    });
}
